package com.bwie.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bwie.common.utils.HttpUtils;
import com.bwie.common.utils.R;
import com.bwie.common.vo.WeiboAccessVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.user.dao.UserDao;
import com.bwie.user.entity.UserEntity;
import com.bwie.user.service.UserService;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public UserEntity login(UserEntity user) {
        String mobile = user.getMobile();

        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);


        return baseMapper.selectOne(wrapper);
    }

    @Override
    public void register(UserEntity user) {
        user.setLogintime(new Date());
        user.setRole(3);
        user.setHighQuality(0);
        baseMapper.insert(user);

    }

    @Override
    public R weiboLogin(WeiboAccessVo weiboAccessVo) throws Exception {
        System.out.println(weiboAccessVo);
        QueryWrapper<UserEntity> w = new QueryWrapper<>();
        w.eq("uaccess",weiboAccessVo.getAccess_token());
        UserEntity entity = baseMapper.selectOne(w);
        if (entity!=null){
            //用户已注册
            entity.setUexpires(weiboAccessVo.getExpires_in()+"");
            entity.setUsocial(weiboAccessVo.getUid());
            //修改
            baseMapper.updateById(entity);
            return R.ok().put("200","登录成功");
        }else {
            //用户没注册过
            //调用远程接口
            HashMap<String, String> map = new HashMap<>();
            map.put("access_token",weiboAccessVo.getAccess_token());
            map.put("uid",weiboAccessVo.getUid());
            HttpResponse httpResponse = HttpUtils.doGet("https://api.weibo.com/2/users/show.json", "", "get", null, map);
            String s = EntityUtils.toString(httpResponse.getEntity());
            JSONObject jsonObject = JSON.parseObject(s);
            UserEntity userEntity = new UserEntity();
            userEntity.setUsocial(weiboAccessVo.getUid());
            userEntity.setUexpires(weiboAccessVo.getExpires_in()+"");
            userEntity.setUaccess(weiboAccessVo.getAccess_token());

            //登录
            userEntity.setUname(jsonObject.get("screen_name").toString());  //用户名
            userEntity.setUheader(jsonObject.get("profile_image_url").toString());//头像

            Date date = new Date();
            userEntity.setLogintime(date);

            baseMapper.insert(userEntity);
            return R.ok().put("200","注册登录成功");
        }

    }

}